package com.frxuexi.document

import android.os.Environment
import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.BaseApp
import com.frxuexi.lib_base.IUiState
import com.frxuexi.lib_base.RequestType
import com.frxuexi.lib_base.entity.LocalDocEntity
import kotlinx.coroutines.launch
import java.io.File

@Composable
fun LocalDocPage(navHostController: NavHostController, vm: DocViewModel = hiltViewModel()) {
    var list = remember {
        SnapshotStateList<LocalDocEntity>()
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect {
                when (it) {
                    is IUiState.onSuccess<*> -> {
                        if(it.type==RequestType.SCAN) {
                            list.addAll(it.data as Collection<LocalDocEntity>)
                        }else if(it.type==RequestType.UPLOAD){
                            ToastUtils.showLong("上传成功")
                            navHostController.popBackStack()
                        }
                    }

                    is IUiState.onError -> {
                        Log.i("===", it.msg)
                    }

                    else -> {}
                }
            }
        }

        vm.sendIntent(DocIntent.scanFile(File("/sdcard/DCIM/Camera")))
//        vm.sendIntent(DocIntent.scanFile(File("/storage/emulated/0/DCIM/Camera")))

//        vm.sendIntent(DocIntent.scanFile(BaseApp.context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!!))
    })

    var fileName by remember {
        mutableStateOf("")
    }

    var path by remember {
        mutableStateOf("")
    }

    Column(Modifier.padding(20.dp)) {
        Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
            Image(
                imageVector = Icons.Default.ArrowBack,
                contentDescription = "",
                Modifier.clickable {
                    navHostController.popBackStack()
                })
            Text(text = "上传文件选择")
            Text(text = "上传",Modifier.clickable {
                vm.sendIntent(DocIntent.uploadFile(fileName,path))
            })
        }
        LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
            items(list) {
                LocalDocItem(entity = it, it.fileName == fileName) {
                    fileName = it.fileName
                    path=it.path
                }
            }
        })
    }
}

@Composable
fun LocalDocItem(entity: LocalDocEntity, isChecked: Boolean, onClick: () -> Unit) {
    /*var isChecked by remember {
        mutableStateOf(false)
    }*/
    Row(Modifier.padding(top = 10.dp)) {
        Box(contentAlignment = Alignment.TopEnd) {
            Image(painter = painterResource(id = R.drawable.icon3), contentDescription = "")
            Image(
                painter = painterResource(id = if (isChecked) R.drawable.ic_action_checked else R.drawable.ic_action_uncheck),
                contentDescription = "",
                Modifier
                    .size(14.dp)
                    .offset(x = 2.dp, -10.dp)
                    .clickable {
//                    isChecked=!isChecked
                        onClick()
                    }
            )
        }
        Column {
            Text(text = entity.fileName)
            Text(
                text = entity.path,
                fontSize = 14.sp,
                color = Color.LightGray,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
        }
    }
}